package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.datastore.core.Serializer;
import easier.data.store.RxProtoStoreManager;
import easier.log.logger.Logger;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import kotlin.Unit;
import kotlin.coroutines.Continuation;

public class MainActivity46 extends AppCompatActivity {

    private EditText mEdit;
    private Button mRead;
    private Button mSave;
    private RxProtoStoreManager< Settings > mProtoStoreManager;
    private Button mDispose;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity46.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main46 );

        initView();
        initViewAction();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();

        mProtoStoreManager.dispose();
    }

    private void initViewAction() {

        mProtoStoreManager = new RxProtoStoreManager<>( this, "settings",
            new SettingsSerializer() );

        mRead.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {

                mProtoStoreManager.observeProto()
                    .map( new Function< Settings, Integer >() {
                        @Override
                        public Integer apply( Settings settings ) throws Throwable {

                            return settings.getExampleCounter();
                        }
                    } )
                    .observeOn( AndroidSchedulers.mainThread() )
                    .subscribe( new Consumer< Integer >() {
                        @Override
                        public void accept( Integer integer ) throws Throwable {

                            Toast.makeText( MainActivity46.this, integer + "", Toast.LENGTH_SHORT ).show();
                        }
                    }, new Consumer< Throwable >() {
                        @Override
                        public void accept( Throwable throwable ) throws Throwable {

                            Logger.e( throwable, "读取失败" );
                            Toast.makeText( MainActivity46.this, "先保存数据", Toast.LENGTH_SHORT ).show();
                        }
                    } );
            }
        } );

        mSave.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {

                mProtoStoreManager.updateData( new Function< Settings, Single< Settings > >() {
                    @Override
                    public Single< Settings > apply( Settings settings ) throws Throwable {

                        return Single.just(
                            settings.toBuilder().setExampleCounter( Integer.parseInt( mEdit.getText().toString() ) ).build() );
                    }
                } );
            }
        } );

        mDispose.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View view ) {

                mProtoStoreManager.dispose();
            }
        } );
    }

    private void initView() {

        mEdit = (EditText) findViewById( R.id.edit );
        mRead = (Button) findViewById( R.id.read );
        mSave = (Button) findViewById( R.id.save );
        mDispose = (Button) findViewById( R.id.dispose );
    }

    private static class SettingsSerializer implements Serializer< Settings > {

        @Override
        public Settings getDefaultValue() {

            return Settings.getDefaultInstance();
        }

        @Nullable
        @Override
        public Object readFrom( @NonNull InputStream inputStream, @NonNull Continuation< ? super Settings > continuation ) {

            try {
                return Settings.parseFrom( inputStream );
            } catch ( IOException e ) {
                e.printStackTrace();
            }
            return getDefaultValue();
        }

        @Nullable
        @Override
        public Object writeTo( Settings settings, @NonNull OutputStream outputStream,
            @NonNull Continuation< ? super Unit > continuation ) {

            try {
                settings.writeTo( outputStream );
            } catch ( IOException e ) {
                e.printStackTrace();
            }

            return settings;
        }
    }
}